using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace SpaceInvaders
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Enemy : Sprite, Boundable
    {
        public static int direction;
        public Projectile projectile;
        private double elapsedTime;
        public  readonly  int DownwardMovement = 12;
        public bool justMovedDown;
        public int movementWaitTime = 900;
        public int movementRate = 20;
        private int frame = 0;
        private Texture2D temp;
        public static bool pause;


        public Enemy(Game game, int width, int height, String location, String locationTwo)
            : base(game, width, height, location,locationTwo)
        {
            velocity = new Vector2(movementRate, 0);
            direction = 1;
        }

        public void Move()
        {
            position += velocity*direction;
        }

        public void MoveDown()
        {
            position.Y += DownwardMovement;
        }

        public bool OutOfBounds()
        {
            switch (direction)
            {
                case 1:
                    return position.X + width + velocity.X >= Game1.screenWidth;
                case -1:
                    return position.X - velocity.X <= 0;
                default:
                    return false;
            }
        }


        public void Fire()
        {
            projectile = new Projectile(Game, 5, 10, "bullet")
            
            { position = new Vector2(position.X + width / 2 - 2, position.Y + height) };
            projectile.direction = -1;
            Game.Components.Add(projectile);
            
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }
        protected override void UnloadContent()
        {
            
            base.UnloadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (!pause)
            {
                elapsedTime += gameTime.ElapsedGameTime.Milliseconds;

                if (elapsedTime > movementWaitTime)
                {
                    if (justMovedDown)
                        justMovedDown = !justMovedDown;
                    else
                    {
                        Move();

                        if (frame%2 == 0)
                        {
                            temp = texture;
                            texture = textureTwo;
                        }
                        else
                        {
                            texture = temp;
                            temp = textureTwo;
                        }
                        frame++;
                    }

                    elapsedTime = 0;
                }

                if (projectile != null && projectile.OutOfBounds())
                {
                    Game.Components.Remove(projectile);
                    projectile = null;

                }
            }

            base.Update(gameTime);
        }
    }
}
